package com.yx.system.controller;

import com.yx.common.annotation.Log;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.ValidatorUtils;
import com.yx.common.validator.group.UpdateGroup;
import com.yx.modules.api.annotation.LoginUser;
import com.yx.modules.api.dao.TbUserMapper;
import com.yx.modules.api.entity.TbUser;
import com.yx.system.dao.TdBookMapper;
import com.yx.system.dao.TdDiscussMapper;
import com.yx.system.dao.TdMessageMapper;
import com.yx.system.entity.SysUser;
import com.yx.system.entity.TdDiscuss;
import com.yx.system.entity.TdMessage;
import com.yx.utils.MessageType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 评论控制器
 *
 * @author xiaoyu
 */
@RestController
@RequestMapping("/sys/discuss")
public class TdDiscussController2 {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private TdDiscussMapper tdDiscussMapper;

    @Autowired
    private TdMessageMapper tdMessageMapper;

    /**
     * 获取评论列表
     */
    @Log("获取评论列表")
    @PostMapping("/list")
    @RequiresPermissions("fac:discuss:list")
    public R list(@LoginUser SysUser sysUserEntity, @RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TdDiscuss> tdDiscusses = tdDiscussMapper.queryList(query);
        int total = tdDiscussMapper.queryTotal(params);
        PageUtils pageUtil = new PageUtils(tdDiscusses, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }


    /**
     * 修改评论
     */
    @Log("修改评论")
    @PostMapping("/update")
    @RequiresPermissions("fac:discuss:update")
    public R update(@RequestBody TdDiscuss tdDiscuss) {
        ValidatorUtils.validateEntity(tdDiscuss, UpdateGroup.class);
        tdDiscussMapper.updateByPrimaryKey(tdDiscuss);
        return R.ok();
    }

    /**
     * 删除评论
     */
    @Log("删除评论")
    @PostMapping("/delete")
    @RequiresPermissions("fac:discuss:delete")
    public R delete(int discussIds) {
        tdDiscussMapper.deleteByPrimaryKey(discussIds);
        List<TdDiscuss> tdDiscussList = tdDiscussMapper.findByReplyId(discussIds);
        for (int i = 0; i < tdDiscussList.size(); i++) {
            tdDiscussMapper.deleteByPrimaryKey(tdDiscussList.get(i).getId());
        }
        return R.ok();
    }

    /**
     * 获取单个评论详情
     */
    @Log("获取单个评论详情")
    @PostMapping("/ById")
    @RequiresPermissions("fac:discuss:ById")
    public R ById(int discussIds) {
        TdDiscuss tdDiscuss = tdDiscussMapper.selectByPrimaryKey(discussIds);
        return R.ok().put("tdDiscussList", tdDiscuss);
    }

    /**
     * 加载评论回复列表
     */
    @Log("加载评论回复列表")
    @PostMapping("/reply")
    @RequiresPermissions("fac:discuss:reply")
    public R reply(int discussIds) {
        List<TdDiscuss> tdDiscussList = tdDiscussMapper.findByReplyId(discussIds);
        return R.ok().put("tdDiscussList", tdDiscussList);
    }

    /**
     * 回复评论
     */
    @Log("回复评论")
    @PostMapping("/adminreply")
    @RequiresPermissions("fac:discuss:adminreply")
    public R adminreply(int discussIds, String content) {
        TdDiscuss tdDiscuss = tdDiscussMapper.selectByPrimaryKey(discussIds);
        TbUser tdUserinfo = tbUserMapper.selectByPrimaryKey(Long.parseLong(tdDiscuss.getUserid() + ""));
        TdMessage tdMessage = new TdMessage("管理员对你的评论做出了回复：" + content, 0, new Date(), MessageType.order.getName(), tdUserinfo.getUuid());
        tdMessageMapper.insert(tdMessage);
        return R.ok();
    }
}
